#include "ParticleCollision.h"
#include <iostream>

ParticleCollision::ParticleCollision(GLfloat xAxis, GLfloat yAxis, GLfloat zAxis)
{
	x = xAxis;
	y = yAxis;
	z = zAxis;
	currentTexture = 0;
	initParticles();
	animationFinished = false;
}

void ParticleCollision::setLocation(GLfloat xAxis, GLfloat yAxis, GLfloat zAxis)
{
	x = xAxis;
	y = yAxis;
	z = zAxis;
	initParticles();
}

void ParticleCollision::setTexture(GLuint t)
{
	currentTexture = t;
}

void ParticleCollision::initParticles()
{
	int i;
	for (i = 1; i < ParticleCount; i++)
	{
		Particle[i].Xpos = x;
		Particle[i].Ypos = y;
		Particle[i].Zpos = z;
		Particle[i].Xmov = rand()%11*0.005 - rand()%11*0.005;
		Particle[i].Zmov = rand()%11*0.005- rand()%11*0.005;
		Particle[i].Red = 1;
		Particle[i].Green = 1;
		Particle[i].Blue = 1;
		Particle[i].Scalez = rand()%8*0.01;
		Particle[i].Direction = 0;
		Particle[i].Acceleration = rand()%10*0.01;
		Particle[i].Deceleration = 0.025;
	}
}

void ParticleCollision::updateParticles()
{
	int i;
	bool tmp = true;
	for (i = 1; i < ParticleCount; i++)
	{
		if (Particle[i].Ypos >= 0)
		{
			Particle[i].Ypos = Particle[i].Ypos + Particle[i].Acceleration - Particle[i].Deceleration;
			Particle[i].Deceleration = Particle[i].Deceleration + 0.0025;
			Particle[i].Xpos = Particle[i].Xpos + Particle[i].Xmov;
			Particle[i].Zpos = Particle[i].Zpos + Particle[i].Zmov;
			Particle[i].Direction = Particle[i].Direction + ((((int)0.5*(rand()%11) + 1)*rand()%11) + 1);
			tmp = false;
		}
	}
	if(tmp)
		animationFinished = true;
}

void ParticleCollision::drawParticles()
{
	int i;
	for (i = 1; i < ParticleCount; i++)
	{
		glPushMatrix();
			glColor3f (Particle[i].Red, Particle[i].Green, Particle[i].Blue);
			glTranslatef(Particle[i].Xpos, Particle[i].Ypos, Particle[i].Zpos);
			glRotatef(Particle[i].Direction - 90, 0, 0, 1);
   
			glScalef (Particle[i].Scalez, Particle[i].Scalez, Particle[i].Scalez);
			glDisable(GL_CULL_FACE);
			glDisable (GL_DEPTH_TEST);
			if(currentTexture)
			{
				glBindTexture (GL_TEXTURE_2D, currentTexture);
			}
			drawCube();

			glEnable(GL_CULL_FACE);
			glEnable(GL_DEPTH_TEST);
		glPopMatrix();
	}
}

void ParticleCollision::animate()
{
	updateParticles();
	drawParticles();
}

bool ParticleCollision::isFinished()
{
	return animationFinished;
}

void ParticleCollision::drawSquare()
{
    glBegin(GL_QUADS);
		glTexCoord2d(0.0,0.0); 
		glVertex2d(-1.0,-1.0);
		glTexCoord2d(1.0,0.0); 
		glVertex2d(1.0,-1.0);
		glTexCoord2d(1.0,1.0); 
		glVertex2d(1.0,1.0);
		glTexCoord2d(0.0,1.0); 
		glVertex2d(-1.0,1.0);
	glEnd();
}

void ParticleCollision::drawCube()
{
	// vertices
	GLfloat A[] = { 0.0f, 1.0f, 1.0f }; 
	GLfloat B[] = { 1.0f, 1.0f, 1.0f }; 
	GLfloat C[] = { 1.0f, 1.0f, 0.0f }; 
	GLfloat D[] = { 0.0f, 1.0f, 0.0f }; 
	GLfloat E[] = { 0.0f, 0.0f, 1.0f }; 
	GLfloat F[] = { 1.0f, 0.0f, 1.0f }; 
	GLfloat G[] = { 1.0f, 0.0f, 0.0f }; 
	GLfloat H[] = { 0.0f, 0.0f, 0.0f }; 

	// texture coordinates
	GLfloat bottomLeft[] = {0.0f, 0.0f};
	GLfloat bottomRight[] = {3.0f, 0.0f};
	GLfloat topLeft[] = {0.0f, 3.0f};
	GLfloat topRight[] = {3.0f, 3.0f};

	glPushMatrix();
		glTranslatef(-0.5, -0.5, -0.5);

		// top
		glBegin(GL_QUADS);
			glNormal3f( 0.0, 1.0, 0.0);
			glTexCoord2fv(bottomLeft);
			glVertex3fv(A);
			glTexCoord2fv(bottomRight);
			glVertex3fv(B);
			glTexCoord2fv(topRight);
			glVertex3fv(C);
			glTexCoord2fv(topLeft);
			glVertex3fv(D);
		glEnd();

		// bottom
		glBegin(GL_QUADS);
			glNormal3f( 0.0, -1.0, 0.0);
			glTexCoord2fv(bottomLeft);
			glVertex3fv(H);
			glTexCoord2fv(bottomRight);
			glVertex3fv(G);
			glTexCoord2fv(topRight);
			glVertex3fv(F);
			glTexCoord2fv(topLeft);
			glVertex3fv(E);
		glEnd();

		// Front
		glBegin(GL_QUADS);
			glNormal3f( 0.0, 0.0, 1.0);
			glTexCoord2fv(bottomLeft);
			glVertex3fv(E);
			glTexCoord2fv(bottomRight);
			glVertex3fv(F);
			glTexCoord2fv(topRight);
			glVertex3fv(B);
			glTexCoord2fv(topLeft);
			glVertex3fv(A);
		glEnd();

		// back
		glBegin(GL_QUADS);
			glNormal3f( 0.0, 0.0, -1.0);
			glTexCoord2fv(bottomLeft);
			glVertex3fv(G);
			glTexCoord2fv(bottomRight);
			glVertex3fv(H);
			glTexCoord2fv(topRight);
			glVertex3fv(D);
			glTexCoord2fv(topLeft);
			glVertex3fv(C);
		glEnd();

		// left
		glBegin(GL_QUADS);
			glNormal3f( -1.0, 0.0, 0.0);
			glTexCoord2fv(bottomLeft);
			glVertex3fv(H);
			glTexCoord2fv(bottomRight);
			glVertex3fv(E);
			glTexCoord2fv(topRight);
			glVertex3fv(A);
			glTexCoord2fv(topLeft);
			glVertex3fv(D);
		glEnd();

		// right
		glBegin(GL_QUADS);
			glNormal3f( 1.0, 0.0, 0.0);
			glTexCoord2fv(bottomLeft);
			glVertex3fv(F);
			glTexCoord2fv(bottomRight);
			glVertex3fv(G);
			glTexCoord2fv(topRight);
			glVertex3fv(C);
			glTexCoord2fv(topLeft);
			glVertex3fv(B);
		glEnd();

	glPopMatrix();
}

ParticleCollision::~ParticleCollision(void)
{
}
